package com.mc.developmentkit.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.qamaster.android.R;
import com.mc.developmentkit.config.MCConstant;
import com.mc.developmentkit.https.HttpUtils;
import com.mc.developmentkit.json.Json;
import com.mc.developmentkit.utils.MCUtils;
import com.mc.developmentkit.utils.ToastUtil;
import com.mc.developmentkit.views.TitleManger;
import org.json.JSONObject;
import org.xutils.x;


/**
 * Created by John_王明阁 on 2016/5/13.
 */
public class ForgetPasswordActivity extends Activity implements View.OnClickListener {

    private TextView tv_QR, tv_YZ, tv_CZ, tv_WC;
    private EditText et_PhoneNum;
    private Button btn_PhoneCommit;

    private TextView tv_NinAccount, tv_PhoneNum, tv_getPhoneCode;
    private EditText et_PhoneCode;
    private Button btn_phoneCodeCommit;

    private EditText et_SettingPassword, et_SettingPasswordAgain;
    private Button btn_PasswordCommit;

    private Button btn_UpdateSuccessToLogin;

    private RelativeLayout re_QR, re_YZ, re_CZ, re_WC;

    private String phoneNum;    //手机号码

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
        TitleManger insetance = TitleManger.getInsetance();
        insetance.setContext(this);
        insetance.setName("忘记密码");
        initView();
    }


    private void initView() {
        tv_QR = (TextView) findViewById(R.id.querenzhanghao);
        tv_YZ = (TextView) findViewById(R.id.yanzhenganquanshezhi);
        tv_CZ = (TextView) findViewById(R.id.chongzhimima);
        tv_WC = (TextView) findViewById(R.id.wancheng);
        et_PhoneNum = (EditText) findViewById(R.id.et_find_qrzhtj);
        btn_PhoneCommit = (Button) findViewById(R.id.btn_find_qrzhtj);
        tv_NinAccount = (TextView) findViewById(R.id.nindezhangha);
        tv_PhoneNum = (TextView) findViewById(R.id.yzaqsz_sjhm);
        tv_getPhoneCode = (TextView) findViewById(R.id.yzaqsz_hqyzm);
        et_PhoneCode = (EditText) findViewById(R.id.et_hqyzm);
        btn_phoneCodeCommit = (Button) findViewById(R.id.btn_tijiao_yanzhengma);
        et_SettingPassword = (EditText) findViewById(R.id.et_stpassword);
        et_SettingPasswordAgain = (EditText) findViewById(R.id.et_setpassword_agi);
        btn_PasswordCommit = (Button) findViewById(R.id.czmitj);
        btn_UpdateSuccessToLogin = (Button) findViewById(R.id.upsucToLogin);
        re_QR = (RelativeLayout) findViewById(R.id.re_qr);
        re_YZ = (RelativeLayout) findViewById(R.id.re_yz);
        re_WC = (RelativeLayout) findViewById(R.id.re_wc);
        re_CZ = (RelativeLayout) findViewById(R.id.re_cz1);

        btn_PhoneCommit.setOnClickListener(this);
        tv_getPhoneCode.setOnClickListener(this);//获取手机验证码
        btn_phoneCodeCommit.setOnClickListener(this);
        btn_PasswordCommit.setOnClickListener(this);
        btn_UpdateSuccessToLogin.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        phoneNum = et_PhoneNum.getText().toString().trim();
        String PhoneCode = et_PhoneCode.getText().toString().trim();
        String SettingPassword = et_SettingPassword.getText().toString().trim();
        String SettingPasswordAgain = et_SettingPasswordAgain.getText().toString().trim();

        int i = v.getId();
        if (i == R.id.btn_find_qrzhtj) {
            if (MCUtils.isMobileNO(phoneNum) == true) {
                tv_QR.setTextColor(x.app().getResources().getColor(R.color.black));
                tv_YZ.setTextColor(x.app().getResources().getColor(R.color.theme_red));
                re_QR.setVisibility(View.GONE);
                re_YZ.setVisibility(View.VISIBLE);
                tv_NinAccount.setText(phoneNum);
                tv_PhoneNum.setText(MCUtils.hidePhoneNumberMiddleFour(phoneNum));
            } else {
                ToastUtil.showToast("输入手机号有误");
            }

        } else if (i == R.id.yzaqsz_hqyzm) {
            TimeCount time = new TimeCount(60000, 1000);// 构造CountDownTimer对象
            time.start();// 开始计时
            try {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("phone", phoneNum);
                HttpUtils.POST(mHandler, MCConstant.getUserGetPhoneCodeUrl(), jsonObject.toString(), true);
            } catch (Exception e) {
                e.printStackTrace();
            }

        } else if (i == R.id.btn_tijiao_yanzhengma) {
            if (!PhoneCode.isEmpty()) {
                tv_YZ.setTextColor(x.app().getResources().getColor(R.color.black));
                tv_CZ.setTextColor(x.app().getResources().getColor(R.color.theme_red));
                re_YZ.setVisibility(View.GONE);
                re_CZ.setVisibility(View.VISIBLE);
            } else {
                ToastUtil.showToast("验证码不能为空");
            }

        } else if (i == R.id.czmitj) {
            if (!SettingPassword.isEmpty() && SettingPassword.length() > 5 && SettingPassword.equals(SettingPasswordAgain)) {
                try {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("phone", phoneNum);
                    jsonObject.put("code", PhoneCode);
                    jsonObject.put("password", SettingPassword);
                    HttpUtils.POST(fHandler, MCConstant.getUserForgetPassword(), jsonObject.toString(), true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (SettingPassword.isEmpty()) {
                ToastUtil.showToast("输入密码不能为空");
            } else if (SettingPassword.length() > 5 == false || SettingPasswordAgain.length() > 5 == false) {
                ToastUtil.showToast("密码长度不得小于六位");
            } else if (!SettingPassword.equals(SettingPasswordAgain)) {
                ToastUtil.showToast("两次密码输入不一致");
            }

        } else if (i == R.id.upsucToLogin) {
           finish();
        }
    }

    // /* 定义一个倒计时的内部类 */
    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
        }

        @Override
        public void onTick(long millisUntilFinished) {
            // 计时过程显示
            tv_getPhoneCode.setClickable(false);
            tv_getPhoneCode.setText(millisUntilFinished / 1000 + "秒");
        }

        @Override
        public void onFinish() {// 计时完毕时触发
            tv_getPhoneCode.setText("重新验证");
            tv_getPhoneCode.setClickable(true);
            tv_getPhoneCode.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("phone", phoneNum);
                        HttpUtils.POST(mHandler, MCConstant.getUserGetPhoneCodeUrl(), jsonObject.toString(), true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    TimeCount time = new TimeCount(60000, 1000);// 构造CountDownTimer对象
                    time.start();// 开始计时

                }
            });
        }

    }

    /**
     * 获取验证码
     */
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    boolean dnScode = Json.DNSJudge("获取短信验证码", msg.obj.toString());
                    if (dnScode == true) {
                       ToastUtil.showToast("验证码获取成功");
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络异常");
                    break;
            }
        }
    };
    /**
     * 找回密码
     */
    Handler fHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    boolean dnScode = Json.DNSJudge("找回密码", msg.obj.toString());
                    if (dnScode == true) {
                        ToastUtil.showToast("找回密码成功");
                        tv_CZ.setTextColor(x.app().getResources().getColor(R.color.black));
                        tv_WC.setTextColor(x.app().getResources().getColor(R.color.theme_red));
                        re_CZ.setVisibility(View.GONE);
                        re_WC.setVisibility(View.VISIBLE);
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络异常");
                    break;
            }
        }
    };

}
